/*
package com.lxinet.jeesns.modules.wechat.web.index;

import com.blade.kit.DateKit;
import com.blade.kit.StringKit;
import com.blade.kit.http.HttpRequest;
import com.blade.kit.json.JSONArray;
import com.blade.kit.json.JSONKit;
import com.blade.kit.json.JSONObject;
import com.lxinet.jeesns.modules.wechat.dao.IWechatMetaDao;
import com.lxinet.jeesns.modules.wechat.entity.WechatContact;
import com.lxinet.jeesns.modules.wechat.entity.WechatMeta;
import com.lxinet.jeesns.modules.wechat.exception.WechatException;
import com.lxinet.jeesns.modules.wechat.listener.WechatListener;
import com.lxinet.jeesns.modules.wechat.service.IWechatMetaService;
import com.lxinet.jeesns.modules.wechat.service.WechatService;
import com.lxinet.jeesns.modules.wechat.utils.CookieUtil;
import com.lxinet.jeesns.modules.wechat.utils.FileUtil;
import com.lxinet.jeesns.modules.wechat.utils.Matchers;
import com.lxinet.jeesns.modules.wechat.utils.SpringBeanUtil;
import com.lxinet.jeesns.utils.JedisUtil;
import com.lxinet.jeesns.utils.SerializeUtil;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;





public class WechatThread extends Thread {

    private IWechatMetaService wechatMetaService;
    private Jedis jedis = JedisUtil.getInstance().getJedis();
    private String uuid;
    private int tip = 0;
    public volatile boolean exit = false;
    public WechatThread(String uuid ,IWechatMetaService wechatMetaService) {
        this.uuid = uuid;
        this.wechatMetaService = wechatMetaService;
    }

    @Override
    public void run() {
            if (StringUtils.hasText(uuid)) {
                WechatMeta wechatMeta = (WechatMeta) SerializeUtil.unserialize(jedis.get(uuid.getBytes()));
                this.tip = 1;
                String url = "https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login";
                Integer cs = 0;//执行次数
                while (wechatMeta.getDlcode() == null && cs < 3 && !exit) {
                    cs++;
                    System.out.println("执行线程" + uuid);
                    HttpRequest request = HttpRequest.get(url, true, "tip", this.tip, "uuid", wechatMeta.getUuid(), "_",
                            DateKit.getCurrentUnixTime());
                    String res = request.body();
                    request.disconnect();
                    if (null != res) {
                        String code = Matchers.match("window.code=(\\d+);", res);
                        if (null != code) {
                            if (code.equals("201")) {
                                this.tip = 0;
                            } else if (code.equals("200")) {
                                tip = 3;
                                String pm = Matchers.match("window.redirect_uri=\"(\\S+?)\";", res);
                                String redirect_uri = pm + "&fun=new";
                                wechatMeta.setRedirect_uri(redirect_uri);
                                String base_uri = redirect_uri.substring(0, redirect_uri.lastIndexOf("/"));
                                wechatMeta.setBase_uri(base_uri);
                                wechatMeta.setDlcode(code);
                                System.out.println("登录成功 " + uuid);
                                login(HttpRequest.get(wechatMeta.getRedirect_uri()), wechatMeta);
                            } else if (code.equals("408")) {

                        }
                    }

                }
                System.out.println("执行线程结束 "+uuid);
            }
        }
    }

*
     * 登录


    public void login(HttpRequest request ,WechatMeta wechatMeta) throws WechatException {
        String res = request.body();
        wechatMeta.setCookie(CookieUtil.getCookie(request));
        request.disconnect();

        if (StringKit.isBlank(res)) {
            throw new WechatException("登录失败");
        }
        wechatMeta.setSkey(Matchers.match("<skey>(\\S+)</skey>", res));
        wechatMeta.setWxsid(Matchers.match("<wxsid>(\\S+)</wxsid>", res));
        wechatMeta.setWxuin(Matchers.match("<wxuin>(\\S+)</wxuin>", res));
        wechatMeta.setPass_ticket(Matchers.match("<pass_ticket>(\\S+)</pass_ticket>", res));

        JSONObject baseRequest = new JSONObject();
        baseRequest.put("Uin", wechatMeta.getWxuin());
        baseRequest.put("Sid", wechatMeta.getWxsid());
        baseRequest.put("Skey", wechatMeta.getSkey());
        baseRequest.put("DeviceID", wechatMeta.getDeviceId());
        wechatMeta.setBaseRequest(baseRequest);
        WechatService wechatService =  SpringBeanUtil.getBean(WechatService.class,"wechatService");
//        微信初始化
        wechatService.wxInit(wechatMeta);
//       微信初始化成功
        //保存微信信息
        jedis.set(uuid.getBytes(), SerializeUtil.serialize(wechatMeta));
        //删除存在记录
        WechatMeta temp = wechatMetaService.findByWXUIN(wechatMeta.getWxuin());
        if(temp!=null){
            wechatMetaService.delete(temp.getId());
        }
        wechatMetaService.save(wechatMeta);
        WechatMeta w1 = wechatMetaService.findByUUID(wechatMeta.getUuid());
//        移除已经存在的登录用户
//        Set set =  jedis.smembers("ydlwxzh");
//        Iterator<String> it = set.iterator();
//        while (it.hasNext()) {
//            String val = it.next();
//            if(val.contains(wechatMeta.getWxuin())){
//                jedis.srem("ydlwxzh",val);
//            }
//        }
        //保存最新的
//        jedis.sadd("ydlwxzh",JSONKit.toJSONString(wechatMeta));
//       开启状态通知...
//        wechatService.openStatusNotify(wechatMeta);
//       开启状态通知成功

//       获取联系人
        WechatContact wechatContact = wechatService.getContact(wechatMeta);
        wechatContact.setWxuin(wechatMeta.getWxuin());
        //保存联系人在redis中(set 惟一 以wxuin为key)
        jedis.set(wechatMeta.getWxuin(), JSONKit.toJSONString(wechatContact));
        String s = jedis.get(wechatMeta.getWxuin().toString());
        WechatContact contact =JSONKit.parse(jedis.get(wechatMeta.getWxuin().toString()),WechatContact.class);
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }
    public void setWechatMetaService(IWechatMetaService wechatMetaService) {
        this.wechatMetaService = wechatMetaService;
    }


*
     * 把联系人对象的 HeadImgUrl 转成base64
     * @param wechatMeta
     * @param wechatContact
     * @return


    private WechatContact imgUrlTOBase64(WechatMeta wechatMeta,WechatContact wechatContact) {
        String cookie=wechatMeta.getCookie();
        JSONArray contactList=wechatContact.getContactList();
        contactList=imgTOBaseHander(cookie,contactList);
        JSONArray memberList=wechatContact.getMemberList();
       // memberList=imgTOBaseHander(cookie,memberList);
        JSONArray groupList=wechatContact.getGroupList();
       // groupList=imgTOBaseHander(cookie,groupList);
        JSONArray groupChatList=wechatContact.getGroupChatList();
        groupChatList=imgTOBaseHander(cookie,groupChatList);
        wechatContact.setContactList(contactList);
        wechatContact.setMemberList(memberList);
        wechatContact.setGroupList(groupList);
        wechatContact.setGroupChatList(groupChatList);
        return wechatContact;
    }

*
     * 遍历联系下面,并把base64字符串替换对应字段HeadImgUrl
     * @param cookie
     * @param jSONArray
     * @return


    private JSONArray imgTOBaseHander(String cookie,JSONArray jSONArray){
        for(int i=0;i<jSONArray.size();i++){
            JSONObject jSONObject=(JSONObject)jSONArray.get(i);
            String headImgUrl= jSONObject.get("HeadImgUrl").toString();
            String imgUrl="";
            try {
                imgUrl= FileUtil.getBase64ByImgFile(cookie,headImgUrl);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(org.apache.commons.lang.StringUtils.isNotBlank(imgUrl)){
                jSONObject.set("HeadImgUrl",imgUrl);
            }
            jSONArray.set(i,jSONObject);
        }
        return jSONArray;
    }

}
*/
